//admin.js
String.prototype.trim=function() {
    return this.replace(/(^\s*)|(\s*$)/g,'');
}
function activeNav(li){
	li.addClass("active");
	li.parent().parent().addClass("active");
	$("#bread-title").text($(li).text());
}

var locationHref = function(url){
	window.location.href = url;
};

var formValid = function(_form){
	$(_form).find('input[data-validate],textarea[data-validate],select[data-validate]').trigger("blur");
	$(_form).find('input[placeholder],textarea[placeholder]').each(function(){
		$hideplaceholder($(this));
	});
	var numError = $(_form).find('.check-error').length;
	if(numError){
		$(_form).find('.check-error').first().find('input[data-validate],textarea[data-validate],select[data-validate]').first().focus().select();
		return false;
	}else{
		return true;
	}
};
var $hideplaceholder=function(element){
	if($(element).data("pintuerholder")){
		$(element).val("");
		$(element).css("color", $(element).data("pintuerholder"));		
		$(element).removeData("pintuerholder");
	}
};

(function($){
	$.extend({
		/**
		 * generator uuid
		 */
		uuid: function() {
			return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
				var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
				return v.toString(16);
			});
		},
		
		/**
		 * alert弹框
		 */
		alert: function(options){
			var uuid = $.uuid();
			var block_id = $.uuid();
			var close_id = $.uuid();
			var ok_id = $.uuid();
			
			var defaults = {
				title: '系统提示',
				message: '系统默认弹框!',
				okText : '确定',
				callback:null
			};
			
			var opts = $.extend(defaults, options);
			
			var tmpl = '<div id="{block_id}"><div id="{uuid}" class="dialog-mask" style="z-index:10000"></div>'
			    + '<div class="dialog-win" style="position: fixed; width: 30%; z-index: 10001;left:35%;top:25%;">'
				+ '<div class="dialog open"><div class="dialog-head">'
				+ '<span id="{close_id}" class="close rotate-hover"></span><strong>{title}</strong></div>'
				+ '<div class="dialog-body" style="max-height:300px;overflow: auto;"> {message} </div><div class="dialog-foot">'
				+ '<button id="{ok_id}" class="button bg-green">{okText}</button>'
				+ '</div></div></div></div>';
			
			tmpl = tmpl.replace('{block_id}', block_id);
			tmpl = tmpl.replace('{uuid}', uuid);
			tmpl = tmpl.replace('{close_id}', close_id);
			tmpl = tmpl.replace('{ok_id}', ok_id);
			tmpl = tmpl.replace('{title}', opts.title);
			tmpl = tmpl.replace('{okText}', opts.okText);
			tmpl = tmpl.replace('{message}', opts.message);
			$('body').append(tmpl);
			
			$("#" + close_id).click(function(){
				$("#" + block_id).remove();
			});
			
			$("#" + ok_id).click(function(){
				$("#" + block_id).remove();
				if(opts.callback){
	            	opts.callback();
	            }
			});
		},
		
		/**
		 * confirm弹框
		 */
		confirm: function(options){
			var uuid = $.uuid();
			var block_id = $.uuid();
			var close_id = $.uuid();
			var ok_id = $.uuid();
			var cancel_id = $.uuid();
			
			var defaults = {
				title: '系统提示',
				message: '系统默认弹框!',
				okText:'确定',
				callback:null,
				cancelText:'取消',
				cancalCallback:null
			};
			var opts = $.extend(defaults, options);
			
			var tmpl = '<div id="{block_id}"><div id="{uuid}" class="dialog-mask" style="z-index:10000"></div>'
				+ '<div class="dialog-win" style="position:fixed;width:30%;z-index:10001;left:35%;top:25%;">'
			    + '<div class="dialog open"><div class="dialog-head">'
			    + '<span id="{close_id}" class="close rotate-hover"></span> <strong>{title}</strong></div>'
			    + '<div class="dialog-body" style="max-height:300px;overflow: auto;"> {message} </div><div class="dialog-foot">' 
			    + '<button id="{ok_id}" class="button bg-green">{okText}</button>&nbsp;&nbsp;'
			    + '<button id="{cancel_id}" class="button dialog-close">{cancelText}</button>'
			    + '</div></div></div></div>';
			
			tmpl = tmpl.replace('{block_id}', block_id);
			tmpl = tmpl.replace('{uuid}', uuid);
			tmpl = tmpl.replace('{close_id}', close_id);
			tmpl = tmpl.replace('{ok_id}', ok_id);
			tmpl = tmpl.replace('{cancel_id}', cancel_id);
			tmpl = tmpl.replace('{okText}', opts.okText);
			tmpl = tmpl.replace('{cancelText}', opts.cancelText);
			tmpl = tmpl.replace('{title}', opts.title);
			tmpl = tmpl.replace('{message}', opts.message);
			$('body').append(tmpl);
			$("#" + close_id).click(function(){
				$("#" + block_id).remove();
			});
			
			$("#" + ok_id).click(function(){
				$("#" + block_id).remove();
				if(opts.callback){
	            	opts.callback();
	            }
			});
			
			$("#" + cancel_id).click(function(){
				$("#" + block_id).remove();
				if(opts.cancalCallback){
	            	opts.cancalCallback();
	            }
			});
		}
	});
})(jQuery);